package com.hualan.sfzy.controller.content;


import com.hualan.sfzy.entity.content.Article;
import com.hualan.sfzy.service.content.ArticleService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService service;
    @PostMapping
    public Result add(@RequestBody Article article){
        return service.add(article)>0?Result.success():Result.error();
    }
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") Integer id){
        return service.remove(id)>0?Result.success():Result.error();
    }
    @DeleteMapping
    public Result removeBatch( Integer[] ids){
        return  service.removeBatch(ids)>0?Result.success():Result.error();
    }
    @PutMapping
    public Result edit(@RequestBody Article article){
        return   service.edit(article)>0?Result.success():Result.error();
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Article article= service.findById(id);
        return Result.success(article);
    }
    @GetMapping("/page")
    public Result findPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize, Article article){
        Map<String,Object> map= service.findByPage(pageNum,pageSize,article);
        return Result.success(map);
    }
    //小程序查询到的文章必须是审核通过的文章
    @GetMapping("/page/status")
    public Result findByPageStatus(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize, Article article){
        Map<String,Object> map= service.findByPageStatus(pageNum,pageSize,article);
        return Result.success(map);
    }

    @GetMapping
    public Result find(){
         List<Article> list= service.find();
        return Result.success(list);
    }
}
